//
//  HMPhotoBrowerCollectionViewCell.swift
//  HMWeiBo
//
//  Created by Apple on 16/8/10.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SDWebImage

class HMPhotoBrowerCollectionViewCell: UICollectionViewCell {
    
    var photoModel: HMPhotoModel?{
    
        didSet{
            
            // 通过SDWebImage 来拿图
            guard  let urlString = photoModel?.thumbnail_pic else {
                return
            }
            
          
            let smallImage =  SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(urlString)
            holerImageView.hidden = false
            // 把小图赋值给holdImageview
            holerImageView.image = smallImage
            
            // 在这里下载大图
            SDWebImageManager.sharedManager().downloadImageWithURL(NSURL(string: replaceImageURLString(urlString)), options: [], progress: { (current, total) -> Void in
                
                print(NSThread.currentThread())
                
                let progress = CGFloat (current) / CGFloat(total)
                
                self.holerImageView.gro = progress
                
                
                }) { (bigImage, error, _ , _ , _ ) -> Void in
                    
                self.bigImageView.image = bigImage
                    
                    // 还没有给bigImageView设置约束和大小
                    self.bigImageView.frame.size = bigImage.size
                   // 设置图片的位置
                    self.setImagePosition(bigImage)
                    
//                    self.holerImageView.hidden = true
                    
            }
        }
        
    }
    
    // 第一步: 重写父类的构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 第二步: 定义一个添加子类和设置子类控件约束的方法
    private func setupUI(){
        contentView.addSubview(scrollView)
        contentView.addSubview(holerImageView)
        
        // 大图要加在 ScrollView里
        scrollView.addSubview(bigImageView)
        
        scrollView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        // 
        holerImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(contentView.snp_center)
        }
        
//        bigImageView.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(scrollView.center)
//        }
        
    }
    
    // 将小图替换成大图
    private func replaceImageURLString(smallString: String) -> String{
        
        // ww3.sinaimg.cn/thumbnail/806c8cthumbnailtvdjvvj20go0ci758.jpg
        // /ww3.sinaimg.cn/bmiddle/806c8cc5gw1f6oetvdjvvj20go0ci758.jpg
        
       return (smallString as NSString).stringByReplacingOccurrencesOfString("/thumbnail/", withString: "/bmiddle/")
    }
    
    //MARK:-- 设置图片的位置
    private func setImagePosition(image: UIImage){
        
        //1. 设置一下图片的宽度,让图片的宽度 = 屏幕的宽度
        let size = image.size
        
        let height = kUIScreenWidth / size.width * size.height
        let width = kUIScreenWidth
        
        bigImageView.frame.size = CGSizeMake(width, height)
        
        
        // 2. 让图片居中显示
        // frame
        if image.size.height > KUIScreenHeight {
            
            
        } else {
            
            // 居中
            
            let top = (KUIScreenHeight - bigImageView.frame.size.height ) * 0.5
            
            scrollView.contentInset = UIEdgeInsets(top: top, left: 0, bottom: 0, right: 0)
        }
        
        scrollView.contentSize = bigImageView.frame.size
        
        
        
    }
    //第三步: 懒加载控件
    //MARK:-- 懒加载控件
    private lazy var holerImageView: HMProgressView = {
    
        let img = HMProgressView(frame: CGRectZero)
        
        img.sizeToFit()
        
        return img
    }()
    /// 滚动的scrollView
    private lazy var scrollView: UIScrollView = UIScrollView()
    /// 大图
    private lazy var bigImageView: UIImageView = UIImageView()
}
